<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <Forpastime@qq.com>
// +----------------------------------------------------------------------
namespace api\tenant\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\db\Query;
use think\facade\Cache;
use think\facade\Request;

class IndexController extends RestBaseController
{
    /**
     * 门店信息
     * @param shop_id 商户ID
     */
    public function info()
    {
        $param = $this->request->param();

        if(!empty($param['shop_id'])){
            $shop = Db::name('shop_info')
                ->field('shop_id,shop_name,shop_address,location,tel,shop_images,category_id,onsale_time,qrcode,checked,scope,services,status,business')
                // ->withAttr('certificate',function($v){
                //     return explode(',',$v);
                // })
                // ->withAttr('other_certificate',function($v){
                //     return explode(',',$v);
                // })
                ->withAttr('services',function($v){
                    return json_decode($v);
                })
                ->withAttr('shop_images',function($v){
                    return explode(',',$v);
                })
                ->where(['shop_id'=>$param['shop_id']])
                ->find();
            
            
            if($shop){
                if($shop['status']==0){
                    $this->error('店铺处于关店状态');
                }
                $services = Db::name('tenant_service')->where('id','in',$shop['services'])->order('list_order asc')->select();
                $shop['services'] = $services;

                $business = Db::name('tenant_business_scope')->where('id','in',json_decode($shop['business']))->order('list_order asc')->select();

                $shop['services'] = $services;
                $shop['business'] = $business;


                if($shop['checked'] == 1){
                    $this->success('请求成功',$shop);
                }else{
                    $this->error('店铺正在审核中...');
                }
            }else{
                $this->error('未找到店铺信息');
            }
        }else{
            $this->error('未找到店铺信息');
        }
    }

    /**
     * 店铺员工
     */
    public function staff()
    {
        $param = $this->request->param();

        if(!empty($param['shop_id'])){
            $tenant = Db::name('shop_apply_order')->alias('o')
                ->leftJoin('shop_info i',['o.shop_info_id = i.id'])
                ->where('i.shop_id',$param['shop_id'])
                ->field('o.*')
                ->find();
            $tenant_id=$tenant['user_id'];

            $category = Db::name('tenant_staff_category')->where('status',1)->select()->toArray();

            $staff = Db::name("tenant_staff")->where('tenant_id',$tenant_id)
                ->where('status',1)
                ->field('id,name,avatar,professional_title,category_id')
                ->order('id asc,category_id asc')
                ->select()
                ->toArray();
            foreach($category as $key => $val){
                $category[$key]['staffs'] = $a = [];

                foreach($staff as $k=>$v){
                    if($v['category_id'] == $val['id']){
                        array_push($category[$key]['staffs'],$v);
                        // $category[$key]['staffs'][$k] = $v;

                    }
                    
                }
            }
            $this->success('请求成功',$category);
        }else{
            $this->error('异常');
        }
    }
    /**
     * 店铺员工详情
     */
    public function staffDetail()
    {
        $param = $this->request->param();

        if(!empty($param['shop_id']) && !empty($param['staff_id'])){
            $tenant = Db::name('shop_apply_order')->alias('o')
                ->leftJoin('shop_info i',['o.shop_info_id = i.id'])
                ->where('i.shop_id',$param['shop_id'])
                ->field('o.*')
                ->find();
            $tenant_id=$tenant['user_id'];

            $staff = Db::name("tenant_staff")->where('tenant_id',$tenant_id)
                ->where('id',$param['staff_id'])
                ->withAttr('identification',function($v){
                    return json_decode($v);
                })
                ->find();
            unset($staff['create_time']);
            unset($staff['tenant_id']);

            $this->success('请求成功',$staff);
        }else{
            $this->error('异常');
        }
    }

   
}